<?php

namespace App\Services;

use \App\Libraries\WeChatPay\WxPayApi;
use \App\Libraries\WeChatPay\Data\WxPayUnifiedOrder;

/**
 * 微信支付服务
 * @package App\Services
 */
class WxPayService {

    #region　统一下单

    /**
     * 统一下单
     * @param $body  商品或支付单简要描述
     * @param $orderNo 商户订单号
     * @param $orderAmount 订单总金额，单位为元
     * @param $outTime 订单过期时长
     * @param string $tradeType 支付类型 (JSAPI，NATIVE，APP)
     * @return \App\Libraries\WeChatPay\成功时返回
     */
    function unifiedOrder($body, $orderNo, $orderAmount, $outTime, $tradeType = "APP") {
        // 统一下单输入对象
        $unifiedOrder = new WxPayUnifiedOrder();
        $unifiedOrder->SetBody($body);
        $unifiedOrder->SetOut_trade_no($orderNo);
        //设置订单总金额，单位为分
        $unifiedOrder->SetTotal_fee($orderAmount * 100);
        $unifiedOrder->SetTrade_type($tradeType);
        $unifiedOrder->SetTime_start(date("YmdHis"));
        $unifiedOrder->SetTime_expire(date("YmdHis", time() + $outTime));

        $result = WxPayApi::unifiedOrder($unifiedOrder);
        $result["timeStamp"] = time();
        $result["package"] = "Sign=WXPay";
        $data = array(
            "appid" => $result["appid"],
            "partnerid" => $result["mch_id"],
            "prepayid" => $result["prepay_id"],
            "noncestr" => $result["nonce_str"],
            "timestamp" => $result["timeStamp"],
            "package" => $result["package"],
        );
        $result["sign"] = $unifiedOrder->MakeSign($data);
        return $result;
    }

    #endregion

}